﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
/// 智能分页结果，包含总记录数、总页数、当前页码、每页显示数量、页码参数名称和当前页的数据集
/// </summary>
public class ParseSqlSource
{
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="count">总数</param>
    /// <param name="takenum">每页显示数量</param>
    /// <param name="pageMark">页码参数名称</param>
    public ParseSqlSource(int count, int takenum, string pageMark = "PageIndex")
    {
        Count = count;
        PageMark = pageMark;
        PageIndex = this.GetRequestInt(PageMark);
        if (PageIndex <= 0) PageIndex = 1;
        TakeNum = takenum;
        if (Count % TakeNum == 0)
            PageCount = Count / TakeNum;
        else
            PageCount = Count / TakeNum + 1;
        Sql = "";
        ConnStr = "";
        ParamValues = null;
    }
    /// <summary>
    /// 初始化
    /// </summary>
    public ParseSqlSource()
    {
        Count = 0;
        PageMark = "PageIndex";
        PageIndex = this.GetRequestInt(PageMark);
        if (PageIndex <= 0) PageIndex = 1;
        TakeNum = 10;
        PageCount = 1;
        Sql = "";
        ConnStr = "";
        ParamValues = null;
    }

    /// <summary>
    /// 当前页显示的数据
    /// </summary>
    public DataTable GetTable()
    {
        if (ConnStr == "")
            return TSQL.ExecDt(Sql, ParamValues);
        else
            return TSQL.ExecDtWithOther(Sql, ConnStr, ParamValues);
    }
    /// <summary>
    /// 所有数据
    /// </summary>
    public DataTable GetOldTable()
    {
        if (ConnStr == "")
            return TSQL.ExecDt(OldSql, ParamValues);
        else
            return TSQL.ExecDtWithOther(OldSql, ConnStr, ParamValues);
    }
    /// <summary>
    /// 总数据行数
    /// </summary>
    public int Count { get; set; }
    /// <summary>
    /// 总数据页数
    /// </summary>
    public int PageCount { get; set; }
    /// <summary>
    /// 当前页码
    /// </summary>
    public int PageIndex { get; set; }
    /// <summary>
    /// 页码参数名称
    /// </summary>
    public string PageMark { get; set; }
    /// <summary>
    /// 每页显示数据行数
    /// </summary>
    public int TakeNum { get; set; }
    /// <summary>
    /// 转换后的读取数据SQL语句
    /// </summary>
    public string Sql { get; set; }
    /// <summary>
    /// 转换前的读取数据SQL语句
    /// </summary>
    public string OldSql { get; set; }
    /// <summary>
    /// 连接数据库字符串
    /// </summary>
    public string ConnStr { get; set; }
    /// <summary>
    /// 传入参数值数组
    /// </summary>
    public object[] ParamValues { get; set; }

    public string PageList(
        int PageNum = 5,
        string PageInfoText = "共 {Count} 条记录 {PageCount} 页 当前第 {PageIndex} 页",
        string PageButtonText = "首页|上页|下页|末页|GO")
    {
        string html = "", midhtml = "";

        if (Count > 0)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath + "?" + PageMark + "={0}";
            HttpRequest collection = HttpContext.Current.Request;
            string[] keys = collection.QueryString.AllKeys;
            foreach (var key in keys)
            {
                if (key.ToLower() != PageMark.ToLower())
                {
                    if (collection.QueryString[key] != null)
                    {
                        url += "&" + key + "=" + HttpContext.Current.Server.UrlEncode(collection.QueryString[key].ToStr());
                    }
                }
            }

            string s0 = "首页";
            string s1 = "上页";
            string s2 = "下页";
            string s3 = "末页";
            string s4 = "GO";
            try
            {
                s0 = PageButtonText.Split('|')[0];
                s1 = PageButtonText.Split('|')[1];
                s2 = PageButtonText.Split('|')[2];
                s3 = PageButtonText.Split('|')[3];
                s4 = PageButtonText.Split('|')[4];
            }
            catch { }

            if (Count == 0) return "";

            html = PageInfoText.Replace("{Count}", Count.ToString()).Replace("{PageCount}", PageCount.ToString()).Replace("{PageIndex}", PageIndex.ToString());

            int k = PageNum / 2;
            int j = PageIndex - k;
            if (j + PageNum > PageCount) j = PageCount - PageNum + 1;
            if (j <= 0) j = 1;
            for (int i = j; i < PageNum + j; i++)
            {
                if (i > PageCount) break;
                midhtml += " <a href=\"" + string.Format(url, i) + "\" style=\"padding:0px 3px;\"" + (PageIndex == i ? " disabled" : "") + ">" + i.ToString() + "</a> ";
            }

            html += "<span><a href=\"" + string.Format(url, 1) + "\" style=\"padding:0px 3px;\"" + (PageIndex <= 1 ? " disabled" : "") + ">" + s0 + "</a> <a href=\"" + string.Format(url, PageIndex - 1) + "\" style=\"padding:0px 3px;\"" + (PageIndex <= 1 ? " disabled" : "") + ">" + s1 + "</a> " + midhtml + " <a href=\"" + string.Format(url, PageIndex + 1) + "\" style=\"padding:0px 3px;\"" + (PageIndex >= PageCount ? " disabled" : "") + ">" + s2 + "</a> <a href=\"" + string.Format(url, PageCount) + "\" style=\"padding:0px 3px;\"" + (PageIndex >= PageCount ? " disabled" : "") + ">" + s3 + "</a> <input type=\"text\" style=\"margin:0px 3px; font-size:12px; width:30px; height:15px;\" attr=\"num\" value=\"" + PageIndex + "\"> <a href=\"javascript:void(0)\" onclick=\"javascript:location.href='" + Regex.Split(url, @"\{0\}")[0] + "'+($(this).parent().find('input').first().val()>=" + PageCount + "?" + PageCount + ":$(this).parent().find('input').first().val())+'" + Regex.Split(url, @"\{0\}")[1] + "'\">" + s4 + "</a></span>";
        }

        return html;
    }
}
